
import { reqGetChannels, reqGetAllChannels } from '@/api/channels'

export default {
  namespaced: true,
  state: {
    channelsList: [],
    allChannelsList: []
  },
  mutations: {
    setChannels(state, channelsList) {
      state.channelsList = channelsList
    },
    setAllChannels(state, channelsList) {
      state.allChannelsList = channelsList
    },
    addChannelItem(state, addChannelItem) {
      state.channelsList.push(addChannelItem)
    },
    delChannelItem(state, index) {
      state.channelsList.splice(index, 1)
    }

  },
  actions: {
    async getChannels(contxet) {
      const res = await reqGetChannels()
      contxet.commit('setChannels', res.data.channels)
    },
    async getAllChannels(contxet) {
      const res = await reqGetAllChannels()
      contxet.commit('setAllChannels', res.data.channels)
    }
  },
  getters: {
    optionChannels(state) {
      // state.allChannelsList.filter(item,  state.allChannelsList)
      // 对总的进行过滤，只保留那些与all没有的
      // state.allChannelsList.findIndex(item.id=>state.channelsList.id)
      const results = state.allChannelsList.filter(item => {
        const index = state.channelsList.findIndex(val => +val.id === +item.id)
        return index === -1
      })
      return results
    }
  }

}
